//
// Created by Yalin on 2021/2/27.
//
#include <math.h>
#include <stdio.h>
#include "calc.h"
#include <string.h>
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];

void push(double f){
    if(sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %d \n", f);
}

double pop(void){
    if(sp > 0)
        return val[--sp];
    else{
        printf("error: stack empty\n");
        return 0.0;
    }
}

//清空栈
void clear(){
    sp = 0;
}

void mathfnc(char s[]){
    double op2;

    if (strcmp(s, "sin") == 0)
        push(sin(pop()));
    else  if (strcmp(s, "cos") == 0)
        push(cos(pop()));
    else  if (strcmp(s, "exp") == 0)
        push(exp(pop()));
    else  if (strcmp(s, "pow") == 0){
        op2 = pop();
        push(pow(pop(), op2));
    }
    else
        printf("error: %s not supported\n", s);
}